﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "IWallProperty.h"
#include "WallProperty.h"
#include "IDocument.h"
#include "UiDocumentViewUtils.h"
#include "IElementParameters.h"
#include "IElement.h"
#include "IParameterDefinitionLibrary.h"
#include "UniIdentity.h"
#include "IParameterDefinition.h"
#include "ui_WallProperty.h"
#include "IUserTransaction.h"
#include "IGenericElement.h"
#include "ParameterDefine.h"
#include "UiCommonDialog.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 


using namespace Sample;
using namespace gcmp;

IWallProperty::IWallProperty(gcmp::IElement* pWallElement)
{
    m_pElement = pWallElement;
}

void IWallProperty::Modify()
{
    WallProperty::Param param;
    IGenericElement *pGenericElement = quick_cast<IGenericElement>(m_pElement);

    IElementParameters* pParamters = pGenericElement->GetElementParameters();
    OwnerPtr<IParameter> opParameterWidth = pParamters->GetNativeParameter(
        GET_UIPLATFORM_PARAMETER_ID(WIDTH_PARAMETER));
    param.dWidth = opParameterWidth->GetValueAsDouble();

    OwnerPtr<IParameter> opParameterHeight = pParamters->GetNativeParameter(
        GET_UIPLATFORM_PARAMETER_ID(HEIGHT_PARAMETER));
    param.dHeight = opParameterHeight->GetValueAsDouble();

    // 构造对话框
    WallProperty *dlg = new WallProperty;
    dlg->setAttribute(Qt::WA_DeleteOnClose);
    dlg->setWindowModality(Qt::ApplicationModal);
    dlg->SetParam(param);
    if (dlg->exec() != QDialog::Accepted)
    {
        return;
    }
    param = dlg->GetParam();
    IDocument *pDocument = UiDocumentViewUtils::GetCurrentDocument();
    OwnerPtr<IUserTransaction> opUserTransaction =
        IUserTransaction::Create(pDocument, GBMP_TR(L"设置墙体"));
    opParameterWidth->SetValueAsDouble(param.dWidth);
    opParameterHeight->SetValueAsDouble(param.dHeight);
    std::wstring errMsg;
    if (pParamters->SetNativeParameter(opParameterWidth.get(), &errMsg) &&
        pParamters->SetNativeParameter(opParameterHeight.get(), &errMsg))
    {
        opUserTransaction->Commit();
    }
    else
    {
        opUserTransaction->Rollback();
        UiCommonDialog::ShowMessageBox(L"error", errMsg, (int)UiCommonDialog::ButtonType::Yes);
    }
}

gcmp::IDocument* Sample::IWallProperty::GetDocument()
{
    return m_pElement->GetDocument();
}

